(function(){
	var config = {
		Event : "click",
		date : 200,
		type : 1
	};
	function XuiForm(){};
	XuiForm.fn = XuiForm.prototype;
	//config
	XuiForm.fn.config = {
		form : ".x-ui-form"
	};
	//checkbox
	XuiForm.fn.checkbox = function(){
		var th = this,
			t,p,
			config = this.config,
			checkbox = $(config.form).find(".form-checkbox input[type=checkbox]"),
			f = {
				icoHtml : '<div class="checkbox-ico iconfont"></div>',
				checkedClass : "checked",
				disabledClass : "disabled",
				//选中
				checked : function(input , p){
					p.addClass(f.checkedClass);
					input.prop("checked" , true);
				},
				//禁用
				disabled : function(p){
					p.addClass(f.disabledClass);
				},
				event : function(p , input){
					p.on("click" , function(){
						input.is(":checked") ? (input.prop("checked" , false) , p.removeClass("checked")) : (f.checked(input , p));
					})
				}
			};
		checkbox.each(function(){
			t = $(this),
			p = t.parent();
			p.addClass("form-checkbox");
			p.append(f.icoHtml);
			//判断是否选中
			t.is(":checked") && f.checked(t , p);
			//判断是否禁用
			t.is(":disabled") ? (f.disabled(p)) : (f.event(p , t));
		});
	};
	//折叠
	XuiForm.fn.fold = function(json , show_callback , hide_callback){
		//继承
		this.config = $.extend(config , json);
		var _config = this.config,
			element = $(_config.element);
		_config.type == 2 && (_config.clssName && $(element[0]).addClass(_config.clssName) , $(element[0]).find(".ico").html("-") , $(element[0]).next(_config.elementS).css("display" , "block"));
		//绑定事件
		element.on(_config.Event , function(){
			if($(this).next(_config.elementS).is(":visible")){
				if(_config.type == 2){
					$(obj_c+":first").show();
					return false;
				}else{
					$(this).next(_config.elementS).slideUp(_config.date);
				};
				//hide_callback
				hide_callback && hide_callback.call(this , $(this));
			}else{
				if(_config.type != 3){
					element.next(_config.elementS).slideUp(_config.date);
					_config.clssName && element.removeClass(_config.clssName);
				};
				//class
				$(this).next(_config.elementS).slideDown(_config.date);
				//show_callback
				show_callback && show_callback.call(this , $(this));
			};
			_config.clssName && $(this).toggleClass(_config.clssName);
		});
	};
	//select
	XuiForm.fn.select = function(){
		var config = this.config,
			t,p,
			select = $(config.form).find(".form-select select"),
			selected = "",
			list = "",
			h = "",
			isDisabled = "",
			option = '',
			element = "",
			f = {
				selected : function(h , t){
					//显示
					h.find(".select-view").on("click" , function(){
						element = $(this).next(".select-list");
						f.selectHiden(element);
						element.slideToggle(200);
					});
					//list
					h.find("li").on("click" , function(){
						var th = $(this);
						//设置val
						t.val(th.data("val"));
						//设置txt
						h.find("i").text(th.text());
						//设置隐藏
						f.selectHiden();
					});
				},
				selectHiden : function(element){
					$(".select-list").not(element).hide();
				}
			};
		select.each(function(){
			//重置list
			list = "";
			t = $(this);
			isDisabled = t.is(":disabled");
			//获取默认选中的
			selected = t.find("option:selected").text();
			for(var i=0,option=t.find("option"); i<option.length; i++){
				list += '<li data-val='+option.eq(i).val()+'>'+option.eq(i).html()+'</li>';
			};
			list = '<ul class="select-list">'+list+'</ul>';
			h = $('<div class="select-box"><span class="select-view'+(isDisabled ? " disabled" : "")+'"><i>'+selected+'</i><span class="ico iconfont"></span></span>'+list);
			t.before(h);
			//事件
			!isDisabled && f.selected(h , t);
		});
	};
	//表单验证
	XuiForm.fn.verification = function(form , btn , rule , msg , callback , enter){
		var t = this,
			config = {
				form : form ? form : t.config.form,
				btn : btn ? btn : ".btn-submit",
				state : "",
				//规则
				rule : {
					//手机
					mobile : function(val){
						return /^(((13[0-9]{1})|(15[0-35-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(val);
					},
					//邮箱
					eamil : function(val){
						return /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/.test(val);
					},
					//电话
					telephone : function(val){
						return /^(\d{3,4}-?)?\d{7,9}$/g.test(val);
					},
					length : function(val , min , max){
						return val.length > min ? (val.length < max ? true : "max") : "min";
					}
				},
				//验证
				submit : function(f){
					var input = f.find('input[required]'),
						name = "",
						val = "",
						state = "",
						th = "",
						rName = "";
					input.each(function(){
						th = $(this),
						name = th.attr("name");
						val = th.val().replace(/(^\s*)|(\s*$)/g,"");
						if(val){
							rName = rule[name];
							if(rName && config.rule[rName]){
								state = config.rule[rName](val);
								return state = config.error.call(this , state , name);
							}else if(rName && typeof rName === "object"){
								state = config.rule["length"](val , rName["min"] , rName["max"]);
								return state = config.error.call(this , state , name);
							}else{
								return state = true;
							};
						}else{
							th.focus();
							xuiModal().msg(msg[name] && msg[name].empty ? (msg[name].empty) : (name+"不能为空"),{
								icon : 2
							});
							return state = false;
						};
					});
					state && callback && callback(t.getData(f));
				},
				error : function(state , name){
					var _msg = "",
						_state = "";
					switch(state){
						case true:
							return true;
							break;
						case false:
							_msg = msg[name].error;
							_state = false;
							break;
						case "min":
							_msg = msg[name].min;
							_state = false;
							break;
						case "max":
							_msg = msg[name].max;
							_state = false;
							break;
					};
					$(this).focus();
					xuiModal().msg(_msg,{
						icon : 2
					});
					return _state;
				}
			};
		$(config.btn).on("click" , function(){
			config.submit($(this).parents(config.form));
		});
		//判断是会否绑定按键
		if(enter){
			$(document).on("keydown" , function(event){
				event.keyCode === 13 && (config.submit.call($(config.btn) , $(config.btn).parents(config.form)));
			});
		};
	};
	XuiForm.fn.getData = function(element){
		var data = {},
			form = $(element),
			d = form.serializeArray();
		//遍历数组
		$.each(d, function(){
			if(data[this.name]){
				data[this.name] = data[this.name] + ","+ this.value;
			}else{
				data[this.name] = this.value;
			};
		});
		return data;
	};
	//接口
	window.XuiForm = XuiForm;
})(window,$);
